package kalender.backend;

import java.util.List;

import javax.persistence.*;


@Entity
public class LuserPic {
	private Luser uid;
	private byte[] picture;
	
	@Id
	@OneToOne
	@JoinColumn(name="uid")
	public Luser getUid() { return uid; }
	public void setUid(Luser l) { uid = l; }
	
	@Lob
	@Basic(fetch=FetchType.LAZY)
	public byte[] getPicture() { return picture; }
	public void setPicture(byte[] p) { picture = p; }
	
	public static LuserPic findByUser(Luser l) throws BackendException {
		try {
			EntityManagerFactory factory = Persistence.createEntityManagerFactory("derp");
			EntityManager em = factory.createEntityManager();
			Query q = em.createQuery("select e from LuserPic e where e.uid = ?1");
			q.setParameter(1, l);
			@SuppressWarnings("unchecked")
			List<LuserPic> rl = q.getResultList();
			return rl == null || rl.size() < 1 ? null : rl.get(0);
		} catch (IllegalStateException e) {
			throw new BackendException("Databasfel. Var god försök igen.", e);
		} catch (IllegalArgumentException e) {
			throw new BackendException("Databasfel. Var god försök igen.", e);
		}
	}
	
	public void saveNew() throws BackendException {
		try {
			EntityManagerFactory factory = Persistence.createEntityManagerFactory("derp");
			EntityManager em = factory.createEntityManager();
			
			em.getTransaction().begin();
			
			em.persist(this);
			
			em.getTransaction().commit();
			
			em.close();
			factory.close();
		} catch (EntityExistsException e) {
			throw new BackendException("Databasfel. Var god försök igen.", e);			
		} catch (IllegalStateException e) {
			throw new BackendException("Databasfel. Var god försök igen.", e);			
		} catch (IllegalArgumentException e) {
			throw new BackendException("Databasfel. Var god försök igen.", e);			
		} catch (TransactionRequiredException e) {
			throw new BackendException("Databasfel. Var god försök igen.", e);		
		}
	}
	
	public void saveUpdate() throws BackendException {
		try {
			EntityManagerFactory factory = Persistence.createEntityManagerFactory("derp");
			EntityManager em = factory.createEntityManager();
			
			em.getTransaction().begin();
			
			em.merge(this);
			
			em.getTransaction().commit();
			
			em.close();
			factory.close();
		} catch (EntityExistsException e) {
			throw new BackendException("Databasfel. Var god försök igen.", e);			
		} catch (IllegalStateException e) {
			throw new BackendException("Databasfel. Var god försök igen.", e);			
		} catch (IllegalArgumentException e) {
			throw new BackendException("Databasfel. Var god försök igen.", e);			
		} catch (TransactionRequiredException e) {
			throw new BackendException("Databasfel. Var god försök igen.", e);		
		}
	}
	
	public static void delete(int uid) throws BackendException {
		try {
			EntityManagerFactory factory = Persistence.createEntityManagerFactory("derp");
			EntityManager em = factory.createEntityManager();
			
			em.getTransaction().begin();
			
			LuserPic lp = em.find(LuserPic.class, uid);
			em.remove(lp);
			
			em.getTransaction().commit();
			
			em.close();
			factory.close();
		} catch (EntityExistsException e) {
			throw new BackendException("Databasfel. Var god försök igen.", e);			
		} catch (IllegalStateException e) {
			throw new BackendException("Databasfel. Var god försök igen.", e);			
		} catch (IllegalArgumentException e) {
			throw new BackendException("Databasfel. Var god försök igen.", e);			
		} catch (TransactionRequiredException e) {
			throw new BackendException("Databasfel. Var god försök igen.", e);		
		}
	}
}
